﻿using System;
using System.Configuration;
using System.Collections.Generic;
using Shuba.Model;
using DynamicComparer;
using System.Web;

namespace Shuba.Web.parameter
{
    public partial class parameter_list : basePage
    {
        string msg = "";
        bool IsView = false, IsAdd = false, IsEdit = false, IsDelete = false, IsChecker = false;

        private void BindData()
        {
            Shuba.Business.AppParam param = new Shuba.Business.AppParam();
            AppParamInfo list;

            list = param.Get();

            hidID.Value = list.ROW_ID.ToString();
            lblGuFUP.Text = list.GU_FUP.ToString();
            lblGuCD.Text = list.GU_CLOSE_DEAL.ToString();
            lblGuFUPFunding.Text = list.GU_FUP_FUNDING.ToString();
            lblGuCDFunding.Text = list.GU_CLOSE_DEAL_FUNDING.ToString();
            lblPurging.Text = list.PURGING_PERIOD.ToString();
            lblPurgWeb.Text = list.PURGING_PERIOD_WEB.ToString();
            lblPass.Text = list.DB_PASS_NEW.ToString();
            lblProcessingDate.Text = list.IS_AUTO_PROCESS_DATE == "1" ? "Auto" : "Manual";
            dtProcessDate.Text = list.PROCESS_DATE.ToString("dd MMMM yyyy");
            lblNPWP.Text = list.NPWP.ToString("n0");
            lblTimeOut.Text = list.TIME_OUT.ToString();
            lblRefresh.Text = list.REFRESH_TIME.ToString();
            lblJobFinalDedup.Text = list.JOB_FINAL_DEDUP.ToString();
            lblFinalDedup.Text = list.IS_NEED_FINAL_DEDUP == "1" ? "Yes" : "No";

            if (IsEdit) btnEdit.Visible = true; else btnEdit.Visible = false;

            //FOR APPROVAL GRID
            if (IsChecker)
            {
                BindApprovalData();

                if (ApprovalGrid.Rows.Count > 0) { ApprovalPnl.Visible = true; } else { ApprovalPnl.Visible = false; }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string acl = GetUserAccess("MSTR_PARAM");

            IsView = IsUserView(acl);
            IsAdd = IsUserAdd(acl);
            IsEdit = IsUserEdit(acl);
            IsDelete = IsUserDelete(acl);
            IsChecker = IsUserChecker(acl);

            if (!IsView)
            {
                msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
            }

            if (!IsPostBack)
            {
                InitSort();
                BindData();

                LabelTitle.Text = GetPageTitle("MSTR_PARAM");
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("parameter_add.aspx?a=ed&id=" + hidID.Value);
        }

        private void BindApprovalData()
        {
            Shuba.Business.AppParam param = new Shuba.Business.AppParam();
            List<AppParamInfo> list;

            list = param.GetListApproval(HttpContext.Current.User.Identity.Name);
            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                DynamicComparer<AppParamInfo> comparer = new DynamicComparer<AppParamInfo>(sort);
                list.Sort(comparer);
            }
            ApprovalGrid.DataSource = list;
            ApprovalGrid.DataBind();
        }
    }
}